package com.km.uploadoss.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.km.uploadoss.R;
import com.km.uploadoss.model.PicInfo;
import com.km.uploadoss.utils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cxw on & 2019/2/20
 */
public class GridImageAdapter extends RecyclerView.Adapter<GridImageAdapter.ViewHolder> {

    public static final int TYPE_PICTURE_ADD = 1;//
    public static final int TYPE_PICTURE = 2;
    private LayoutInflater mInflater;
    private List<PicInfo> mPicturePathList = new ArrayList<>();// 图片路径容器
    private int mSelectMax = 9;// 最多选择数量
    private Context mContext;
    private OnAddPicClickListener mOnAddPicClickListener;
    private RequestOptions options = new RequestOptions()
            .fitCenter()
            .placeholder(R.mipmap.ic_pic_load)
            .error(R.mipmap.ic_pic_no_image);

    public GridImageAdapter(Context context, OnAddPicClickListener onAddPicClickListener) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        this.mOnAddPicClickListener = onAddPicClickListener;
    }

    public void setPicturePathList(List<PicInfo> list) {
        this.mPicturePathList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_filter_image,
                viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_PICTURE_ADD) {
            viewHolder.iv_pic.setImageResource(R.mipmap.ic_pic_add);
            viewHolder.iv_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddPicClickListener.OnAddPic();
                }
            });

            viewHolder.iv_del.setVisibility(View.GONE);
        } else {
            viewHolder.iv_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddPicClickListener.OnPreview(position);
                }
            });
            viewHolder.iv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddPicClickListener.OnDelPic(position);
                }
            });
            PicInfo mPicPath = mPicturePathList.get(position);

            Glide.with(mContext)
                    .asBitmap()
                    .apply(options)
                    .load(mPicPath.getOriginalPath())
                    .into(viewHolder.iv_pic);

            Bitmap mBitmap = BitmapUtils.compressPixel(mPicPath.getOriginalPath());
            viewHolder.iv_pic.setImageBitmap(mBitmap);

            switch (mPicPath.getUploadState()) {
                case 0:
                    viewHolder.iv_del.setVisibility(View.VISIBLE);
                    viewHolder.pb_upload.setVisibility(View.GONE);
                    viewHolder.tv_upload_progress.setVisibility(View.GONE);
                    break;
                case 1:
                    viewHolder.iv_del.setVisibility(View.GONE);
                    viewHolder.pb_upload.setVisibility(View.VISIBLE);
                    viewHolder.tv_upload_progress.setVisibility(View.VISIBLE);

                    viewHolder.pb_upload.setProgress(mPicPath.getUploadProgress());
                    viewHolder.tv_upload_progress.setText(mPicPath.getUploadProgress() + "%");
                    break;
                case 2:
                    viewHolder.iv_del.setVisibility(View.GONE);
                    viewHolder.pb_upload.setVisibility(View.VISIBLE);
                    viewHolder.tv_upload_progress.setVisibility(View.VISIBLE);

                    viewHolder.pb_upload.setProgress(mPicPath.getUploadProgress());
                    viewHolder.tv_upload_progress.setText("上传完成!");
                    break;
                case 3:
                    viewHolder.iv_del.setVisibility(View.GONE);
                    viewHolder.pb_upload.setVisibility(View.VISIBLE);
                    viewHolder.tv_upload_progress.setVisibility(View.VISIBLE);

                    viewHolder.pb_upload.setProgress(100);
                    viewHolder.tv_upload_progress.setText("重新上传");
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mPicturePathList.size() < mSelectMax) {
            return mPicturePathList.size() + 1;
        } else {
            return mPicturePathList.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_PICTURE_ADD;
        } else {
            return TYPE_PICTURE;
        }
    }

    private boolean isShowAddItem(int position) {
        int size = mPicturePathList.size() == 0 ? 0 : mPicturePathList.size();
        return position == size;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_pic;
        ProgressBar pb_upload;
        TextView tv_upload_progress;
        ImageView iv_del;

        public ViewHolder(View view) {
            super(view);
            iv_pic = view.findViewById(R.id.iv_pic);
            pb_upload = view.findViewById(R.id.pb_upload);
            tv_upload_progress = view.findViewById(R.id.tv_upload_progress);
            iv_del = view.findViewById(R.id.iv_del);
        }
    }

    public interface OnAddPicClickListener {
        void OnAddPic();// 添加
        void OnDelPic(int position);// 删除
        void OnPreview(int position);// 查看大图
    }

}
